//
//  SubscribeNewsCell.m
//  suRead
//
//  Created by lidi on 2018/12/5.
//  Copyright © 2018 bao. All rights reserved.
//

#import "SubscribeNewsCell.h"
#import "M_News.h"
#import "NewCellManager.h"
#import "AccountManager.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"

@interface SubscribeNewsCell()
@property (weak, nonatomic) IBOutlet UIView *viewCountContainer;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewIcon;
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (weak, nonatomic) IBOutlet UILabel *labelCreateTime;
@property (weak, nonatomic) IBOutlet UILabel *labelSource;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *consImageViewIcon;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *consImageViewIconWidth;
@end

@implementation SubscribeNewsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    if ( [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0) {
        self.layoutMargins = UIEdgeInsetsZero;
        self.preservesSuperviewLayoutMargins = NO;
        self.separatorInset = UIEdgeInsetsMake(0, 15.0f, 0, 15.0f);
    }
    
    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = [UIColor colorWithRed:0xee / 255.0f green:0xee / 255.0f blue:0xee / 255.0f alpha:1];
    [self.contentView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomLine.superview).offset(15.0);
        make.right.equalTo(bottomLine.superview).offset(-15.0);
        make.bottom.equalTo(bottomLine.superview);
        make.height.equalTo(@.5f);
    }];
}

- (void)setNewsData:(id)newsData
{
    M_News *news = (M_News *)newsData;
    
    NSString *imageURL = news.f_thumbnail;
    if (imageURL && [imageURL isKindOfClass:[NSString class]] && [imageURL length] > 0) {
        self.imageViewIcon.hidden = NO;
        self.consImageViewIcon.constant = 15;
        self.consImageViewIconWidth.constant = 80;
        [self.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"lazy"]];
    } else {
        self.imageViewIcon.hidden = YES;
        self.consImageViewIcon.constant = 0;
        self.consImageViewIconWidth.constant = 0;
    }
    
    NSString *title = news.f_title;
    self.labelTitle.text = title;
    
    //已读未读
    if ([[NewCellManager shared] hasRead:news.f_id]) {
        self.labelTitle.textColor = [UIColor lightGrayColor];
    } else {
        self.labelTitle.textColor = [UIColor blackColor];
    }
    
    NSString *createTime = news.f_updateTime;
    self.labelCreateTime.text = createTime;
    
    if ([AccountManager sharedAccountManager].f_isShowViewCount) {
        self.viewCountContainer.hidden = NO;
        self.labelViewCount.text = news.f_views;
        [self.zanLable setText:[NSString stringWithFormat:@"%ld",news.f_square]];
    } else {
        self.viewCountContainer.hidden = YES;
    }
    
    self.labelSource.text = news.f_source;
}

@end
